import { computed, ref } from 'vue'
import type { PageableHook, PageInfo } from '@/types'

/**
 * 分页内容
 * 建议和 useModel 一起使用
 * useModel({}, {
 *   format: {
 *     pageable
 *   }
 * })
 */
export default function usePageable(size = 10) {
  const pageable = {
    page: 1,
    size,
    total: 0,
  } as PageableHook

  Object.defineProperty(pageable, 'reset', {
    value() {
      this.page = 1
      this.size = size
    },
  })

  Object.defineProperty(pageable, 'resetPage', {
    value() {
      this.page = 1
    },
  })

  Object.defineProperty(pageable, 'getReq', {
    value() {
      return <PageInfo>{
        pageSize: this.size,
        pageNum: this.page,
      }
    },
  })
  const state = ref(pageable)

  return computed<PageableHook>({
    get() {
      return state.value
    },
    set(value) {
      state.value.page = value.page
      state.value.total = value.total
      state.value.size = value.size
    },
  })
}
